package com.arsn.myf.common.exception;

import com.arsn.myf.common.enums.ResultCode;
import com.arsn.myf.common.util.result.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * 全局异常处理器
 * @author zhongrj
 * @since 2022-08-04
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常处理
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public R serviceErrorHandler(ServiceException e){
        e.printStackTrace();
        return R.errorException(e.getMessage());
    }

    /**
     * excel导入导出异常处理
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler(value = ExcelException.class)
    @ResponseBody
    public R excelExceptionHandler(ExcelException e){
        e.printStackTrace();
        return R.errorException(e.getMessage());
    }

    /**
     * 自定义异常处理
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public R customExceptionHandler(CustomException e){
        e.printStackTrace();
        return R.errorException(e.getMessage());
    }

    /**
     * 默认异常
     * @param e 异常对象
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R defaultHandler(Exception e){
        e.printStackTrace();
        return R.errorException(ResultCode.FAILURE);
    }
}
